package com.alpha.runes.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.appcompat.widget.AppCompatImageView;

import com.alpha.runes.Common;
import com.alpha.runes.R;
import com.alpha.runes.util.SharedPreferenceUtils;

public class ViewManager {

    private int screenHeight;
    private int screenWidth;
    private WindowManager.LayoutParams wmParams;

    private RunesFloatBall floatingMagnetView;

    private int dpi;
    private View view;
    private AppCompatImageView removeBallView;
    private SharedPreferences preferences;
    private ImageView guideTwoImageView;



    private ViewManager() {
    }

    private static ViewManager viewManager;

    public static ViewManager getInstance() {
        if (viewManager == null) {
            synchronized (ViewManager.class) {
                viewManager = new ViewManager();
            }
        }
        return viewManager;
    }


    public void init(Activity mContext, MagnetViewListener magnetViewListener) {
        preferences = mContext.getPreferences(Context.MODE_PRIVATE);

        view = LayoutInflater.from(mContext).inflate(R.layout.activity_float_item, null);

        floatingMagnetView = view.findViewById(R.id.alphaspeed_floaticon_btn);
        floatingMagnetView.setMagnetViewListener(magnetViewListener);

        removeBallView = view.findViewById(R.id.alphaspeed_remove_ball);

        guideTwoImageView = view.findViewById(R.id.alphaspeed_guide_2);

        Boolean isShowGuideTwo = (Boolean) SharedPreferenceUtils.getData(preferences, Common.IS_SHOW_GUIDE_TWO, true);
        guideTwoImageView.setVisibility(isShowGuideTwo ? View.VISIBLE : View.GONE);
        SharedPreferenceUtils.putData(preferences, Common.IS_SHOW_GUIDE_TWO, false);

        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int widthPixels = dm.widthPixels;
        int heightPixels = dm.heightPixels;

        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

        //屏宽
        screenWidth = wm.getDefaultDisplay().getWidth();
        //屏高
        screenHeight = wm.getDefaultDisplay().getHeight();
        //通过像素密度来设置按钮的大小
        dpi = dpi(dm.densityDpi);

        wmParams = new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
        wmParams.format = PixelFormat.RGBA_8888;//设置背景图片
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;//
        wmParams.x = widthPixels; //设置位置像素

        wmParams.width = widthPixels; //设置图片大小
        wmParams.height = heightPixels;
        wm.addView(view, wmParams);
    }


    public AppCompatImageView getRemoveBallView() {
        return removeBallView;
    }

    public void showRemoveBallView() {

        if (removeBallView.isShown()) {
            return;
        }
        removeBallView.setVisibility(View.VISIBLE);
    }

    public void hideRemoveBallView() {
        removeBallView.setVisibility(View.GONE);
    }


    public void hideGuideTwoImageView() {
        guideTwoImageView.setVisibility(View.GONE);
    }

    /**
     * 根据密度选择控件大小
     */
    private int dpi(int densityDpi) {
        if (densityDpi <= 120) {
            return 36;
        } else if (densityDpi <= 160) {
            return 48;
        } else if (densityDpi <= 240) {
            return 72;
        } else if (densityDpi <= 320) {
            return 96;
        }
        return 108;
    }
}
